home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / langs / iconv8_l.arc / PROCS.ARC / tuple.icn < prev    next >
Encoding:
Text File  |  1990-03-05  |  2.3 KB  |  62 lines

  1. ############################################################################
  2. #
  3. #   Name:    tuple.icn
  4. #
  5. #   Title:    Process n-tuples
  6. #
  7. #   Author:    William H. Mitchell
  8. #
  9. #   Date:    June 10, 1988
  10. #
  11. ############################################################################
  12. #
  13. #     This procedure implements a "tuple" feature that produces the effect
  14. #  of multiple keys.  A tuple is created by an expression of the
  15. #  form
  16. #
  17. #    tuple([exrp1, expr2, ..., exprn])
  18. #
  19. #  The result can be used in a case expression or as a table subscript.
  20. #  Lookup is successful provided the values of expr1, expr2, ..., exprn
  21. #  are the same (even if the lists containing them are not).  For example,
  22. #  consider selecting an operation based on the types of two operands.  The
  23. #  expression
  24. #
  25. #    case [type(op1), type(op2)] of  {
  26. #       ["integer", "integer"]:  op1 + op2
  27. #       ["string", "integer"] :  op1 || "+" || op2
  28. #       ["integer", "string"] :  op1 || "+" || op2
  29. #       ["string", "string"]  :  op1 || "+" || op2
  30. #       }
  31. #
  32. #  does not work, because the comparison in the case clauses compares lists
  33. #  values, which cannot be the same as control expression, because the lists
  34. #  are different, even though their contents are the same.  With tuples,
  35. #  however, the comparison succeeds, as in
  36. #
  37. #    case tuple([type(op1), type(op2)]) of {
  38. #       tuple(["integer", "integer"]):  op1 + op2
  39. #       tuple(["string", "integer"]) :  op1 || "+" || op2
  40. #       tuple(["integer", "string"]) :  op1 || "+" || op2
  41. #       tuple(["string", "string"])  :  op1 || "+" || op2
  42. #       }
  43. #
  44. ############################################################################
  45.  
  46. procedure tuple(tl)
  47.    static tuptab
  48.    initial tuptab := table()    # create the root node
  49.  
  50.    /tuptab[*tl] := table()    # if there is no table for this size, make one
  51.    tb := tuptab[*tl]        # go to tuple for size of table
  52.    i := 0            # assign default value to i
  53.    every i := 1 to *tl - 1 do {    # iterate though all but last value
  54.       e := tl[i]        # ith value in tuple
  55.       /tb[e] := table()        # if it is not in the table, make a new one
  56.       tb := tb[e]        # go to table for that value
  57.       }
  58.    le := tl[i + 1]        # last value in tuple
  59.    /tb[le] := copy(tl)        # if it is new, entr a copy of the list
  60.    return tb[le]        # return the copy; it is unique
  61. end
  62.